package org.shunfeng.controller;

import org.shunfeng.pojo.SysUser;
import org.shunfeng.service.DeliveryListService;
import org.shunfeng.service.UserService;
import org.shunfeng.service.impl.DeliveryListServiceImpl;
import org.shunfeng.service.impl.UserServiceImpl;

import javax.jws.WebService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Map;

@WebServlet("/delivery/updateDelivery")
public class UpdateDelivery extends HttpServlet {
    private DeliveryListService deliveryListService = new DeliveryListServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            req.setCharacterEncoding("utf-8");
            resp.setContentType("text/html;charset=utf-8");
            SysUser sysUser = (SysUser) req.getSession().getAttribute("user");
            Integer userId = sysUser.getId();
            System.out.println("userId = " + userId);
            Map<String, String[]> parameterMap = req.getParameterMap();
            int count = deliveryListService.updateDelivery(userId, parameterMap);
            if ( count > 0 ) {
                resp.sendRedirect(req.getContextPath() + "/delivery/list");
            }else {
                resp.sendRedirect(req.getContextPath() + "/delivery/editDelivery");
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
